// pages/me/completeRegister/completeRegister.js
const app=getApp();
var md5 = require("../md5.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focusName:false,
    focusPwd:false,
    inputNameValue:'',
    inputPwdValue:'',
    ispassword: true,
    temp:0,
    isShow:true,
    imgSrc:'../../../images/user_img.png',
    token:'',
    telephone:'',
    imgType:"png",
    filename:"../../../images/user_img.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        token: options.token,
        telephone:options.telephone
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 输入密码验证函数
  CheckPassWord:function(password) {//必须为字母加数字且长度不小于8位
    var str = password;
    if (str == null || str.length < 6) {
      return -1;
    }
    var reg1 = new RegExp(/^[0-9A-Za-z]+$/);
    if (!reg1.test(str)) {
      return -2;
    }
    var reg = new RegExp(/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/);
    if (reg.test(str)) {
      return true;
    } else {
      return false;
    }
  },
  // 头像
  cameraBtn:function(){
    var that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePath = res.tempFilePaths[0];
        var imgName = tempFilePath.split("tmp/");
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success(res) {
           that.setData({
             imgType: res.type
           })
          }
        })
        console.log(res);
        that.setData({
          imgSrc: tempFilePath,
          filename: imgName[1]
        })
      }
    })
  },
  // 昵称
  bindNameTap: function () {
    this.setData({
      focusName: true
    })
  },
  inputNameNum: function (e) {
    this.setData({
      inputNameValue: e.detail.value,
      showCancel: true,
      disabled: false
    })
  },
  // 密码
  bindPwdTap: function () {
    this.setData({
      focusPwd: true
    })
  },
  inputPwdNum: function (e) {
    this.setData({
      inputPwdValue: e.detail.value,
      showCancel: true,
      disabled: false
    })
  },
  // 是否显示密码
  isShowBtn:function(){
    var that=this;
      that.setData({
        ispassword: true,
        isShow: true
      })
  },
  isHideBtn: function () {
    var that = this;
    console.log("aaa");
    that.setData({
      ispassword: false,
      isShow: false
    })
  },
  // 完成
  bindCompleteBtn:function(){
    var that=this;
    var baseCode="";
    // 图片转base64
    wx.getFileSystemManager().readFile({
      filePath: that.data.imgSrc, //选择图片返回的相对路径
      encoding: 'base64', //编码格式
      success: res => { //成功的回调
        console.log('data:image/png;base64,' + res.data)
        baseCode = res.data;
      }
    })
    if (that.data.inputNameValue == '' || that.data.inputNameValue==null){
        wx.showToast({
          title: '请输入昵称',
          icon: "none",
          duration: 2000
        })
        return;
    } 
    var temp=that.CheckPassWord(that.data.inputPwdValue);
    if(temp==-1){
      wx.showToast({
        title: '最少输入6位数',
        icon: "none",
        duration: 2000
      })
      return;
    }else if(temp==-2){
      wx.showToast({
        title: '请使用6〜16个数字和字母的组合',
        icon: "none",
        duration: 2000
      })
      return;
    }else{
      var myDate = new Date();
      var timestamp = new Date().getTime();
        // 用户注册接口
        wx.request({
          url: 'https://papi.zcyun.cn/v1/user/regist',
          method: 'POST',
          data: {
            telephone: that.data.telephone,
            token: that.data.token,
            nickname: that.data.inputNameValue,
            password: md5.md5(that.data.inputPwdValue),
            platformId: app.globalData.platformId,
            filename: that.data.filename,
            avatar: 'data:image/' + that.data.imgType + ';base64,' + baseCode
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res.data);
            if (res.data.code == 0) {
              app.globalData.openId = res.data.openId;
              app.globalData.openKey = res.data.openKey;
              wx.redirectTo({
                url: '../pages/login/login'
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
            }
          }
        })
    }
  }
})